﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Service.Core.Files;
using Service.Core.Filters;
using Service.Framework.AppStartup;
using Service.Framework.Utils;

namespace Service.Core.Shared
{
    /// <summary>
    /// 基础启动器
    /// </summary>
    /// <typeparam name="TStartupMoudle"></typeparam>
    [ImportStartupModule(typeof(SharedStartup))]
    public class AppStartupBase<TStartupMoudle> : AppStartupBaseCore<TStartupMoudle> where TStartupMoudle : IStartupModule, new()
    {
        /// <summary>
        /// 注册服务
        /// </summary>
        /// <param name="webApplicationBuilder"></param>
        public override void ConfigureServices(WebApplicationBuilder webApplicationBuilder)
        {
            base.ConfigureServices(webApplicationBuilder);
            webApplicationBuilder.Services
                .AddControllers(options =>
                {
                    options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true; // 对于非空引用类型，不自动添加 Required 属性
                    options.Filters.Add<GlobalExceptionFilter>();
                    options.Filters.Add<ApiResultFilter>();
                }).ConfigureApiBehaviorOptions(options =>
                {
                    options.SuppressModelStateInvalidFilter = true;
                })
                .AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.ContractResolver = new CustomContractResolver();
                    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                    //options.SerializerSettings.Converters.Add(new StringEnumConverter());
                });
        }

        /// <summary>
        /// 使用服务
        /// </summary>
        /// <param name="webApplication"></param>
        public override void Configure(WebApplication webApplication)
        {

            webApplication.UseRouting();
            // 使用跨域
            webApplication.UseCrossOrigin();
            webApplication.UseAuthentication();
            webApplication.UseAuthorization();
            webApplication.MapControllers().RequireCors();
            base.Configure(webApplication);
        }
    }

}
